<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use App\Models\Image;
use Illuminate\Support\Facades\Storage;



class ImageController extends Controller
{
    protected $imageModel;
    protected $userModel;

    public function __construct(){
        $this->userModel = new User();
        $this->imageModel = new Image();
    }

    public function getTabBar(){
        $res = $this->imageModel->getInfoListByType(1);
        return $this->success($res,'tabBar信息获取成功');
    }

    public function getBanner(){
        $res = $this->imageModel->getInfoByType(2);
        return $this->success($res,'Banner信息获取成功');
    }


    public function getStore(){
        $res = $this->imageModel->getInfoByType(3);
        return $this->success($res,'门店页头图获取成功');
    }



    /**
     * 上传头像到七牛云
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function uploadAvatarToQiNiu(Request $request){
        $imageObj = $request->image;
        $openid = $request->input('openid');
        //获取文件的后缀
        $ext = $imageObj->getClientOriginalExtension();

        //定义存放在七牛云的图片的路径和文件名
        $key = "avatar/".time().".".$ext;
        //储存图片到本地
        $path = $imageObj->store('images');

        //上传文件到七牛云
        $res = $this->QiNiuUploadFile($key,$path);
        //删除本地图片节省项目空间
        Storage::delete($path);
        //将上传后的七牛云图片链接保存在数据库中
        $data = [
            'avatarUrl' => config('qiniu.CNAME')."/".$res[0]['key']
        ];
        $userInfo = $this->userModel->saveData('openid',$openid,$data);

        return $this->success($userInfo,"图片上传成功");

    }
}
